import { getUserDetails, getUserInfo, login } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
const state = {
  token: getToken(),
  userInfo: {}
}
const mutations = {
  setToken(state, token) {
    state.token = token
    setToken(token)
  },
  setUserInfo(state, payload) {
    state.userInfo = payload
  },
  setPhoto(state, photo) {
    state.userInfo.staffPhoto = photo
  },
  removeToken(state) {
    state.token = ''
    removeToken()
  },
  removeUserInfo(state) {
    state.userInfo = {}
  }
}
const actions = {
  async login(context, payload) {
    const res = await login(payload)
    context.commit('setToken', res)
  },
  async getUserInfo(context) {
    const res = await getUserInfo()
    const data = await getUserDetails(res.userId)
    context.commit('setUserInfo', {
      ...res,
      ...data
    })
  },
  logout(context) {
    context.commit('removeToken')
    context.commit('removeUserInfo')
    resetRouter()
    context.commit('permission/setRouter', [], { root: true })
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}

